<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource {
    protected $showProtectedFields = false;
    public function toArray($request) {
        if(!$this->showProtectedFields){
            $this->resource->makeHidden(['phone','email']);
        }
        //138****0001 ->  ->
        $data = parent::toArray($request);
        $data['fake_phone'] = $this->resource->phone ?
            substr_replace($this->resource->phone,'****',3,4):
            false;
        $data['fake_email'] = $this->resource->email ?
            substr_replace($this->resource->email,'****',2,4):
            false;
        $data['roles'] = RoleResource::collection($this->whenLoaded('roles'));
        return $data;
    }

    public function showProtectedFields(){
        $this->showProtectedFields = true;
        return $this;
    }
}
